package de.consileon.resisweb.client;

import org.gwtopenmaps.openlayers.client.LonLat;
import org.gwtopenmaps.openlayers.client.Map;
import org.gwtopenmaps.openlayers.client.MapOptions;
import org.gwtopenmaps.openlayers.client.MapWidget;
import org.gwtopenmaps.openlayers.client.control.LayerSwitcher;
import org.gwtopenmaps.openlayers.client.control.MousePosition;
import org.gwtopenmaps.openlayers.client.control.NavToolbar;
import org.gwtopenmaps.openlayers.client.control.PanZoomBar;
import org.gwtopenmaps.openlayers.client.layer.Layer;
import org.gwtopenmaps.openlayers.client.layer.OSM;
import org.gwtopenmaps.openlayers.client.layer.WMS;
import org.gwtopenmaps.openlayers.client.layer.WMSOptions;
import org.gwtopenmaps.openlayers.client.layer.WMSParams;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GWTResisOpenLayers implements EntryPoint {
	
	private static GWTResisOLUiBinder uiBinder = GWT
    .create(GWTResisOLUiBinder.class);

    interface GWTResisOLUiBinder extends
            UiBinder<Widget, GWTResisOpenLayers> {
    }
    
    private MapOptions defaultMapOptions;
	//private MapWidget mapWidget;
	private Map map;
	private MapWidget mapWidget	;
	
	
	public static final String WMS_URL = "http://labs.metacarta.com/wms/vmap0";
	private WMS wmsLayer;
	private OSM osmLayer;
    
    @UiField
    VerticalPanel navigationPanel;

    @UiField
    VerticalPanel mapPanel;
    
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);

	public GWTResisOpenLayers(){
		this.defaultMapOptions = new MapOptions();
		//In OL, the map gets PanZoom, Navigation, ArgParser, and Attribution Controls
		//by default. Do removeDefaultControls to remove these.
//		this.defaultMapOptions.removeDefaultControls();
		this.defaultMapOptions.setNumZoomLevels(16);
		this.defaultMapOptions.setProjection("EPSG:4326");
		initMapWidget(this.defaultMapOptions);
	}
	
	private void initMapWidget(MapOptions options){
		mapWidget = new MapWidget("550px","550px", defaultMapOptions);
		this.map = mapWidget.getMap();
//		OSM osm_1 = OSM.Osmarender("Osmarender"); 
//		osm_1.setIsBaseLayer(true);
//		osmLayer=OSM.Mapnik("MapNick");
//		map.addLayer(osm_1);
		
//		addBingMaps(map);
		//Defining a WMSLayer and adding it to a Map
		WMSParams wmsParams = new WMSParams();
		wmsParams.setFormat("image/png");
		wmsParams.setLayers("basic");
		wmsParams.setStyles("");

		WMSOptions wmsLayerParams = new WMSOptions();
		wmsLayerParams.setUntiled();
		//wmsLayerParams.setTransitionEffect(TransitionEffect.RESIZE);

		
		wmsLayer = new WMS(
				"Basic WMS",
				WMS_URL,
				wmsParams,
				wmsLayerParams);
		
		map.addLayers(new Layer[] {wmsLayer});

		//Adding controls to the Map
		map.addControl(new PanZoomBar());
		//use NavToolbar instead of deprecated MouseToolbar
		map.addControl(new NavToolbar());
		map.addControl(new MousePosition());
		map.addControl(new LayerSwitcher());

		//Center and Zoom
		double lon = 11.0;
		double lat = 53.0;
		int zoom = 14;
		map.setCenter(new LonLat(lon, lat), zoom);
		
	}
	
	public static native void addBingMaps(Map map)/*-{
	map.addControl(new OpenLayers.Control.LayerSwitcher());
	var shaded = new OpenLayers.Layer.VirtualEarth("Shaded", {
                type: VEMapStyle.Shaded
            });
            var hybrid = new OpenLayers.Layer.VirtualEarth("Hybrid", {
                type: VEMapStyle.Hybrid
            });
            var aerial = new OpenLayers.Layer.VirtualEarth("Aerial", {
                type: VEMapStyle.Aerial
            });
 
            map.addLayers([shaded, hybrid, aerial]);
 
            map.setCenter(new OpenLayers.LonLat(-110, 45), 3);
	}-*/;
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		RootLayoutPanel.get().add(uiBinder.createAndBindUi(this));
		mapPanel.clear();
//		contentPanel.add(new Label("Hallo Welt"));
		mapPanel.add(mapWidget); 
	}
}
